package cn.edu.cuit.mockfns.fam.transition;

import cn.edu.cuit.mockfns.fam.tuples.CommonNullEvent;
import cn.edu.cuit.mockfns.fam.tuples.Event;
import cn.edu.cuit.mockfns.fam.tuples.State;

public class TransitionMatcher
{
    private Transition transition;
    private State fromState;
    private Event<?> event;

    public TransitionMatcher(State fromState, Event<?> event, Transition transition)
    {
        this.fromState = fromState;
        this.event = event;
        this.transition = transition;
    }

    /**
     * This method estimates that the inputed event matches current state or not. As a result, it
     * will return matched <Code>Transition</code> or <code>null</code>
     * */
    public Transition match( State currentState, Event<?> event )
    {
        try
        {
            if( this.fromState.getClass() != currentState.getClass() || !event.equals( this.event ) )
            {
                return null;
            }
        }
        catch( NullPointerException e )
        {
            if( this.event instanceof CommonNullEvent )
            {
                return transition;
            }
            return null;
        }

        return transition;
    }
}
